# -*- coding:utf-8 -*-
from PIL import Image
import requests
from bs4 import BeautifulSoup
import re
from io import BytesIO
import os
import time
import json
import Tkinter

print ('Hanzi Test nihao对对对')

# 所有接口
host = 'qc.jhc9888.net'
origin = 'http://'+host
url_auth = origin+'auth?url=/Account/Login/'
# 登录cookie获取
url_login = origin+'/Account/Login/'
# 请求校验码JS
url_captcha = origin+'/Account/Captcha/'
# 生成验证码
url_gen = origin+'/DefaultCaptcha/Generate?t='
# 登录校验
url_login_verify = origin+'/Account/LoginVerify/'
# 余额
url_get_wallet_amount = origin+'/Home/GetWalletAmount'
# 投注
url_bet_confirm = origin+'/Bet/Confirm?tgid='

# 构造Request headers
user_agent = 'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)'

# print (url_auth)

session = requests.session()

# 请求login接口
headers = {
        'User-Agent': user_agent,
        'Host': host,
        'Referer': 'http://qc.jhc9888.net/auth?url=/Account/Login/'
}
resp2 = session.get(url=url_login, headers=headers)
set_cookie_login = resp2.headers.get('Set-Cookie')
# 从Set-Cookie中取到__RequestVerificationToken的值
print (set_cookie_login)
token_key = '__RequestVerificationToken'
arr = set_cookie_login.split(';')
token_pair = arr[0]
# arr2 = arr[0].split('=')
# token = arr2[1]
print (token_pair)

# 请求验证码的js代码
headers_captcha = {
    'User-Agent': user_agent,
    'Host': host,
    'Referer': url_login,
    'Cookie': token_pair,
    'Content-Type': 'application/x-www-form-urlencoded; Charset=UTF-8'
}
data_c = {'CaptchaError': 'False'}
resp_captcha = session.post(url=url_captcha, data=data_c, headers=headers_captcha)
print (resp_captcha.status_code)
print (resp_captcha.headers)
# 把Set-Cookie里的ASP.NET_SessionId取出来
set_cookie_captcha = resp_captcha.headers.get('Set-Cookie')
session_id_key = 'ASP.NET_SessionId'
arr2 = set_cookie_captcha.split(';')
session_id_pair = arr2[0]
print (session_id_pair)
# print (resp_captcha.text)
captcha_soup = BeautifulSoup(resp_captcha.text, 'html.parser')
capt_input = captcha_soup.find('input')
print (capt_input)
t = capt_input.get('value')
captcha_detext_value = t
print (t)

# tag_detext = captcha_soup.find('input', {'id': 'CaptchaDeText'})
# captcha_detext_value = tag_detext.get('value')


# 请求验证码图片
headers_generate = {
    'User-Agent': user_agent,
    'Host': host,
    'Referer': url_login,
    'Cookie': session_id_pair+';'+token_pair,
    'Content-Type': 'application/x-www-form-urlencoded; Charset=UTF-8'
}
resp_gen = session.get(url_gen+t, headers=headers_generate)

image = Image.open(BytesIO(resp_gen.content))
image.save('D:/desktop/captcha.gif')
os.system(r"start D:/desktop/captcha.gif")

# print (resp_gen.text)

# 输入内容
login_id_value = raw_input('请输入用户名')
print (login_id_value)
password_value = raw_input('请输入密码')
print (password_value)
captcha_value = raw_input('请输入验证码')

print (captcha_value)

# 请求登录验证
headers_login_verify = {
    'User-Agent': user_agent,
    'Host': host,
    'Referer': url_login_verify,
    'Cookie': session_id_pair+';'+token_pair,
    'Content-Type': 'application/x-www-form-urlencoded; Charset=UTF-8'
}
print (headers_login_verify)
token_key_value = token_pair.split('=')[1]
data_login_verify = {
    token_key: token_key_value,
    'LoginID': login_id_value,
    'Password': password_value,
    'CaptchaDeText': captcha_detext_value,
    'CaptchaInputText': captcha_value
}
print (data_login_verify)
resp_login_verify = \
    session.post(url_login_verify, data=data_login_verify, headers=headers_login_verify, allow_redirects=False)
# 需要是302
print (resp_login_verify.status_code)
print (resp_login_verify.headers)

# 从header的Set-Cookie获取ApplicationCookie
set_cookie_login_verify = resp_login_verify.headers.get('Set-Cookie')
application_cookie_key = '.AspNet.ApplicationCookie'
arr3 = set_cookie_login_verify.split(';')
# application_cookie_value = arr3['application_cookie_key']
application_cookie_pair1 = ''
application_cookie_pair = ''
for var1 in arr3:
    if application_cookie_key in var1:
        application_cookie_pair1 = var1
        break
arr4 = application_cookie_pair1.split(', ')
for var1 in arr4:
    if application_cookie_key in var1:
        application_cookie_pair = var1
        break
print (application_cookie_pair)


def postBetConfirm():
    headers_bet_confirm = {
        'User-Agent': user_agent,
        'Host': host,
        'Referer': origin+'/Bet/Index/19',
        'Cookie': token_pair+';'+session_id_pair+';'+application_cookie_pair,
        'Content-Type': 'application/json; Charset=UTF-8',
        'Accept': 'application/json,text/javascript,*/*,q=0.01'
    }
    print ('------------------app cookie---------------')
    print (headers_bet_confirm)
    tgid_value = 'd081fa75-e60f-3bc4-3e02-92611c16ea96'
    data_bet = {
        'LotteryGameID': 19,
        'SerialNumber': '645142',
        'Bets': [{
            'BetTypeCode': 3023,
            'BetTypeName': '',
            'Number': '03',
            'Position': '',
            'unit': 0.01,
            'Multiple': 1,
            'ReturnRate': 0,
            'IsCompressed': False
        }],
        'Schedules': [],
        'StopIfWin': False,
        'Guid': tgid_value
    }
    #json_value = json.dumps(data_bet, ensure_ascii=False)
    #print (json_value)
    resp_bet_confirm = session.post(url_bet_confirm+tgid_value, json=data_bet, headers=headers_bet_confirm)
    print (resp_bet_confirm)
    return
# wallet
#while True:

resp_get_wallet_amount = session.post(url_get_wallet_amount)
print (resp_get_wallet_amount.text)
#time.sleep(10)
postBetConfirm()
isBet = raw_input('0下注 1不下注')
if isBet == 0:
    print ('下注')
else:
    print ('不下注')
# account mgr
# bet/confirm